

import java.util.Stack;


public class ExToOs {
	
	
	
	
	public void changeOs(char[][]A)
	{
		Stack<int[]> mystack = new Stack<int[]>();
		if(A.length == 1)
		{ 
			for(char x : A[1]){
			System.out.print(x + ' ');
			}
		}else{
			
			for(int i = 0; i < A.length; i++)
			{
				for(int j = 0; j< A[1].length; j++)
				{
					if( i== 0 || j == 0 || i == A.length-1 || j == A[1].length-1){
						
						if(A[i][j]== 'o'){	
							A[i][j] = 'D';
							mystack.push(new int[]{i,j});
							//While stack in not empty
							while(!mystack.isEmpty()){
								int[] currentpos = mystack.peek();
								if(currentpos[1]+1 < A[1].length && A[currentpos[0]][currentpos[1]+1] =='o')
								{
									A[currentpos[0]][currentpos[1]+1] = 'D';
									mystack.push(new int[]{currentpos[0],currentpos[1]+1});
								}else if(currentpos[0]-1 > -1 && A[currentpos[0]-1][currentpos[1]] =='o')
								{
									A[currentpos[0]-1][currentpos[1]] ='D';
									mystack.push(new int[]{currentpos[0]-1,currentpos[1]});
						
								}else if(currentpos[1]-1 > -1 && A[currentpos[0]][currentpos[1]-1] =='o')
								{
									A[currentpos[0]][currentpos[1]-1] ='D';
									mystack.push(new int[]{currentpos[0],currentpos[1]-1});
								}else if(currentpos[0]+1 < A.length && A[currentpos[0]+1][currentpos[1]] =='o')
								{
									A[currentpos[0]+1][currentpos[1]] ='D';
									mystack.push(new int[]{currentpos[0]+1,currentpos[1]});
								}else{
									mystack.pop();
								}
							}
							
						}	
					}
				}
					
			}
			for(int i = 0; i < A.length; i++)
			{
				for(int j = 0; j< A[1].length; j++)
				{
					if(A[i][j] == 'D')
					{
						A[i][j] = 'o';
					}else if(A[i][j] == 'o')
					{
						A[i][j] = 'x';
					}
					
				}
			}
				
			/*	
			for(char[]r : A)
			{
				for(char c : r)
				{
					if(c == 'D')
					{
						c = 'o';
					}else if(c == 'o')
					{
						c = 'x';
					}
				}
			}
			*/
			
		}
		
	}
	
	public static void main(String[] args)
	{
		char[][] board = new char[][]{
				{'x', 'x', 'x', 'x', 'x'},
				{'x', 'x', 'o', 'o', 'x'},
				{'x', 'x', 'x', 'o', 'x'},
				{'x', 'o', 'o', 'x', 'x'},
				{'x', 'o', 'o', 'x', 'o'},
				{'x', 'x', 'o', 'x', 'o'}
		};
		
		System.out.println("Before :");
		for(char[] r : board)
		{
			for(char c : r)
			{
				System.out.print(c + " ");
			}
			System.out.println();
		}
		
		ExToOs myboard = new ExToOs();
		myboard.changeOs(board);
		
		System.out.println("After :");
		for(char[] r : board)
		{
			for(char c : r)
			{
				System.out.print(c + " ");
			}
			System.out.println();
		} 
		
	}
	
}
